/****************************************************
		File Name:59-4-client.c
		Author: Abby Cin
		Mail: abbytsing@gmail.com
		Created Time: Sun 19 Oct 2014 03:17:19 PM CST
*****************************************************/
#include "59-4.h"
#include <stdio.h>

int main (int argc, char *argv[])
{
    int number, sfd, ipaddr, loop;
    struct sockaddr_in addr;
    char *name=NULL, op;
    int num, choice, saved_errno=errno;
/* close standard input/output buf for printf("enter name") */
    setbuf(stdout,NULL);
    
	name=malloc(DATA_SIZE);
    inet_pton(AF_INET,argv[1],&ipaddr);
    
    socklen_t len = sizeof (struct sockaddr_in);
    bzero (&addr, sizeof (struct sockaddr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons (30902);
    addr.sin_addr.s_addr = (ipaddr == 0) ? INADDR_ANY : ipaddr;
    sfd = socket (AF_INET, SOCK_DGRAM, 0);
	
	printf("Chioces list below; enter your choice\n");
	printf("1; open a existed file.\n");
	printf("2; create a file.\n");
	printf("3; quit.\n: ");
	scanf("%d",&choice);
	sendto (sfd, &choice, sizeof (int), 0, (SA *)&addr, len);
    errno = 0;
	if(choice == 1)
	{
		printf("enter file name (your name) : \t");
		read(STDIN_FILENO,name,DATA_SIZE);
		sendto (sfd, name, DATA_SIZE, 0, (SA *)&addr, len);
		memset(name,0,DATA_SIZE);
        recvfrom(sfd,&errno,sizeof(errno),0,NULL,0);
        
        switch(errno)
        {
            case EBADF:
                puts("File Was Damaged!!!");
                printf("Because above ERROR, file has been deleted by server.\n");
                exit(1);
            case ENOENT:
                perror("ERROR");
                exit(1);
            default:
                errno = saved_errno;
                break;
        }
	}
	else if(choice == 2)
	{
		printf("enter a file name: ");
		read(STDIN_FILENO,name,DATA_SIZE);
		sendto (sfd, name, DATA_SIZE, 0, (SA *)&addr, len);
		memset(name,0,DATA_SIZE);
		printf ("How many people do you want to add to list ?\n:\t");
		scanf ("%d", &num);
		sendto (sfd, &num, sizeof (num), 0, (SA *)&addr, len);

		while (num--)			/* get all date needed by creating a list */
		{
	/* get number */
			printf ("enter your number: ");
			scanf ("%d", &number);
			sendto (sfd, &number, sizeof (int), 0, (SA *)&addr, len);
	/* get name */
			printf ("enter your name: ");
			read(STDIN_FILENO,name,DATA_SIZE);
			sendto (sfd, name, DATA_SIZE, 0, (SA *)&addr, len);
			memset (name, 0, DATA_SIZE);
		}
	}
	else
		exit(0);
    
	printf ("\e[32m###########################################\e[0m\n");
    printf ("'h': show list\n'o': modify list\n'd': delete an element\n");
    printf ("'n': add an element to list\n'f': find and print a node of list\n");
    printf("'0': clean display\n");
	int enable_hint = 1;
    for (;;)
    {
		if(enable_hint == 1)
		{
			printf("Enter choice[q to quit]:\t");
			enable_hint = 0;
		}
        scanf ("%c", &op);
        if(op == 'q')
        {
            sendto (sfd, &op, sizeof (op), 0, (SA *)&addr, len);
            break;
        }
        sendto (sfd, &op, sizeof (op), 0, (SA *)&addr, len);
        switch (op)
	    {
            case 'h':
                recvfrom(sfd,&loop,sizeof(number),0,NULL,0);  
                printf("------------- start --------------\n");
                while(loop--)
                {
                    recvfrom(sfd,&number,sizeof(number),0,NULL,0);  
                    recvfrom(sfd,name,DATA_SIZE,0,NULL,0); /* don't care server's addrinfo */
                    printf("number: %d\tname: %s\n",number,name);
                    memset(name,0,DATA_SIZE);
                }
                printf("*************** end ****************\n");
				enable_hint = 1;
                break;
	        case 'o':
        /* get number */
	            printf ("Enter number of a person: ");
	            scanf ("%d", &number);
				sendto (sfd, &number, sizeof (number), 0, (SA *)&addr, len);
        /* get name to modify */
	            printf ("Enter name you wanna modify: ");
                read(STDIN_FILENO,name,DATA_SIZE);
	            sendto (sfd, name, DATA_SIZE, 0, (SA *)&addr, len);
	            memset (name, 0, DATA_SIZE);
				enable_hint = 1;
	            break;
	        case 'd':
                printf("Enter a person's number that you wanna delete: ");
	            scanf ("%d", &number);
	            sendto (sfd, &number, sizeof (number), 0, (SA *)&addr, len);
				enable_hint = 1;
	            break;
	        case 'n':
                printf("Enter number of person you wanna add: ");
	            scanf ("%d", &number);
	            sendto (sfd, &number, sizeof (number), 0, (SA *)&addr, len);

                printf("Enter his/her name: ");
                read(STDIN_FILENO,name,DATA_SIZE);
	            sendto (sfd, name, DATA_SIZE, 0, (SA *)&addr, len);
	            memset (name, 0, DATA_SIZE);
				enable_hint = 1;
	            break;
	        case 'f':
                printf("Enter who you wanna find: ");
	            scanf ("%d", &number);
	            sendto (sfd, &number, sizeof (number), 0, (SA *)&addr, len);
				enable_hint = 1;
	            break;
            case '0':
                system("clear");
				enable_hint = 1;
                break;
	      //  default:
	        //    break;
	    }
    }
}
